<?php
// behaviors/LastActivityBehavior.php
namespace app\behaviors;

use Yii;
use yii\base\Behavior;
use yii\web\Controller;

class LastActivityBehavior extends Behavior
{
    public function events()
    {
        return [
            Controller::EVENT_BEFORE_ACTION => 'updateLastActivity',
        ];
    }

    public function updateLastActivity()
    {
        if (!Yii::$app->user->isGuest && Yii::$app->user->identity->getIsActive()) {
            $user = Yii::$app->user->identity;
            $user->last_activity = time();
            $user->save(false, ['last_activity']);
        }
    }
}